package com.hjx.windowtool.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class XiaoshuoTest3 {
	public static void main(String[] args) throws Exception {
		
		String dir = "E:\\book\\5";
		if (!new File(dir).exists() || !new File(dir).isDirectory()) {
			new File(dir).mkdirs();
		}

		javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
		TrustManager tm = new miTM();
		trustAllCerts[0] = tm;
		javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
		sc.init(null, trustAllCerts, null);
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
		
//		 Connection conn = Jsoup.connect("https://m.aikanshu8.com/book/3215.html");
//		 conn.header("User-Agent", XiaoShuoTest2.userAgent);
//		 Document d = conn.get();
//		// System.out.println(d);
//		 Element element = d.select("#list-chapterAll").get(0);
//		 Elements select = element.select("a");
//		 List<String> listUrl=new ArrayList<>();
//		 List<String> listTitle=new ArrayList<>();
//		 for (Element e : select) {
////		 System.out.println(e.attr("abs:href")+"==="+e.text());
//		 listUrl.add(e.attr("abs:href"));
//		 listTitle.add(e.text());
//		 }
//		 Files.write(Paths.get(dir+"//url.text"), listUrl);
//		 Files.write(Paths.get(dir+"//titel.text"), listTitle);
//		
//		
//		loadBookContentAikanshu8(dir);
//		
//		
//		XiaoShuoTest2.packageChm(dir, "3206");
	}

	/**
	 * @param dir
	 * @throws IOException
	 */
	public static void loadBookContentAikanshu8(String dir) throws IOException {
		List<String> list = Files.readAllLines(Paths.get(dir + "//url.text"));
		for (int i = 0; i < list.size(); i++) {
			try {
				String url = list.get(i);
				System.out.println(i + "===" + url);
				Connection conn = Jsoup.connect(url);
				conn.header("User-Agent", XiaoShuoTest2.userAgent);
				Document d = conn.get();
//				System.out.println(d);
//				System.out.println(d.select("#htmlContent").get(0).html());
				Elements select = d.select(".imagecontent");
				if (select.size() > 0) {
					String attr = select.attr("src");
					System.out.println(attr);
					XiaoShuoTest2.downloadImage(attr, dir + "//" + i + ".gif", "https://wap.dulaidw.com/wapbook-147995-35931049/");
				} else {
					// System.out.println(select);
					String html = d.select("#htmlContent").get(0).html().replaceAll("&nbsp;", " ");
					String[] split = html.split("<br>");
					List<String> listContents = new ArrayList<>();
					for (String string : split) {
						if (StrUtil.isEmpty(string)) {
							continue;
						}
						listContents.add(string);
					}
					Files.write(Paths.get(dir + "//" + i + ".txt"), listContents);
				}
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
				i--;
			}
		}
	}

	public static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
			return;
		}

		public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
			return;
		}
	}
}
